﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Resources;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.WorkCenters
{
    /// <summary>
    /// 反填工作中心信息及其 资源行信息
    /// </summary>
    public class WorkCenterReverseHandler : IRequestHandler<WorkCenterReverseCommand, Result>
    {
        private readonly IBaseRepository<WorkCenter> WorkCenterRepository;
        private readonly IBaseRepository<WorkCenterWealth> WorkCenterWealthRepository;
        private readonly IBaseRepository<Resource> ResourceRepository;
        private readonly ILogger<WorkCenterUpdApproveHandler> logger;

        public WorkCenterReverseHandler(ILogger<WorkCenterUpdApproveHandler> logger, IBaseRepository<WorkCenter> workCenterRepository, IBaseRepository<WorkCenterWealth> workCenterWealthRepository, IBaseRepository<Resource> resourceRepository)
        {
            this.logger = logger;
            WorkCenterRepository = workCenterRepository;
            WorkCenterWealthRepository = workCenterWealthRepository;
            ResourceRepository = resourceRepository;
        }

        public async Task<Result> Handle(WorkCenterReverseCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                var workCentersSql = $"select * from workCenters WHERE WorkCenterId = {request.workCenterId};";
                var workCentersWealthSql = $"select * from workCentersWealth WHERE WorkCenterId = {request.workCenterId};";
                var WorkCenterres = WorkCenterRepository.GetAll(workCentersSql);
                var ResourceIdSql = $"select * from Resource ;";
                var list = from a in WorkCenterWealthRepository.GetAll(workCentersWealthSql)
                           join b in ResourceRepository.GetAll(ResourceIdSql)
                           on a.WorkCenterWealthName equals b.ResourceName
                           where a.IsDelWorkCenterWealth == false
                           select new
                           {
                               a.WorkCenterWealthId,
                               a.WorkCenterId,
                               b.ResourceId,
                               a.UsableNum,
                               a.Abstract,
                               a.WorkCenterWealthState,
                               a.CreateTime,
                               a.CreateName,
                               a.IsDelWorkCenterWealth
                           };
                var data = new
                {
                    WorkCenterres,
                    list
                };
                if (WorkCenterres.Count() > 0 || list.Count() > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = data;
                    result.Msg = "反填信息成功";
                    logger.LogInformation($"反填工作中心信息及其资源行信息成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Search;
                    result.Data = null;
                    result.Msg = "反填信息失败";
                    logger.LogError($"反填工作中心信息及其资源行信息失败");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"反填工作中心信息及其资源行信息失败,错误信息为：" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
